home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 25 user and custom controls / customcontrollibrary / multiplier.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  3.2 KB  |  107 lines

  1. ' This control shows how you can create a composite control
  2.  
  3. Imports System.ComponentModel
  4. Imports System.Web.UI
  5. Imports System.Web.UI.WebControls
  6.  
  7. <ToolboxData("<{0}:Multiplier runat=server></{0}:Multiplier>")> _
  8. Public Class Multiplier
  9.     Inherits System.Web.UI.WebControls.WebControl
  10.     Implements INamingContainer
  11.  
  12.     ' These variables hold 
  13.     Dim txtFirst As TextBox
  14.     Dim txtSecond As TextBox
  15.     Dim txtResult As TextBox
  16.     Dim WithEvents btnEval As Button
  17.  
  18.     Sub New()
  19.         MyBase.New()
  20.         Me.Width = Unit.Pixel(200)
  21.     End Sub
  22.  
  23.     ' this is the method to override to create all the
  24.     ' constituent control
  25.  
  26.     Protected Overrides Sub CreateChildControls()
  27.         'Create all child controls .
  28.         txtFirst = New TextBox()
  29.         txtSecond = New TextBox()
  30.         btnEval = New Button()
  31.         txtResult = New TextBox()
  32.         Dim lblAsterisk As New Label()
  33.  
  34.         ' Set their properties
  35.         lblAsterisk.Text = " * "
  36.         btnEval.Text = " = "
  37.         txtResult.ReadOnly = True
  38.  
  39.         ' Establish correct width for text controls.
  40.         AdjustControlWidth()
  41.  
  42.         ' Add to the controls collection.
  43.         Controls.Add(txtFirst)
  44.         Controls.Add(lblAsterisk)
  45.         Controls.Add(txtSecond)
  46.         Controls.Add(btnEval)
  47.         Controls.Add(txtResult)
  48.     End Sub
  49.  
  50.     ' This property returns the result of the multiplication
  51.  
  52.     <Browsable(False)> _
  53.     Property Result() As Double
  54.         Get
  55.             ' ensure child control exist before accessing one of them
  56.             EnsureChildControls()
  57.             Try
  58.                 Return CDbl(txtResult.Text)
  59.             Catch
  60.                 Return 0
  61.             End Try
  62.         End Get
  63.         Set(ByVal Value As Double)
  64.             ' ensure child control exist before accessing one of them
  65.             EnsureChildControls()
  66.             txtResult.Text = Value.ToString
  67.         End Set
  68.     End Property
  69.  
  70.     ' this method does the actual rendering
  71.  
  72.     Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
  73.         ' Ensure the child controls exist and then render them.
  74.         EnsureChildControls()
  75.         RenderChildren(output)
  76.         ' Adjust their width.
  77.         AdjustControlWidth()
  78.     End Sub
  79.  
  80.     ' Adjust controls' width.
  81.  
  82.     Private Sub AdjustControlWidth()
  83.         ' Evaluate the space available for the three textboxes.
  84.         Dim w As Unit = Unit.Pixel(CInt(Me.Width.Value - 50) \ 3)
  85.         txtFirst.Width = w
  86.         txtSecond.Width = w
  87.         txtResult.Width = w
  88.     End Sub
  89.  
  90.     ' this event fires when the user clicks on the Eval button
  91.  
  92.     Private Sub btnEval_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEval.Click
  93.         ' ensure child control exist before accessing them
  94.         EnsureChildControls()
  95.         Try
  96.             ' attempt the multiplication
  97.             Dim res As Double = CDbl(txtFirst.Text) * CDbl(txtSecond.Text)
  98.             ' assign a value if successful
  99.             txtResult.Text = res.ToString
  100.         Catch
  101.             ' show an error message otherwise
  102.             txtResult.Text = "# ERR #"
  103.         End Try
  104.     End Sub
  105.  
  106. End Class
  107.